package Unicart.applogic2;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

import static org.openqa.selenium.support.ui.ExpectedConditions.*;

import Unicart.applogic.UserHelper;
import Unicart.model.User;

public class UserHelper2 extends DriverBasedHelper implements UserHelper {
	
	private ApplicationManager2 manager;
	
	public UserHelper2(ApplicationManager2 manager) {
	    super(manager.getWebDriver());
	    this.manager = manager;
	  }
	
	@Override
	public void loginAs(User user) {
		if (driver.findElements(By.linkText("Регистрация")).size() > 0){
	    	driver.findElement(By.linkText("Войти")).click();
	    }
		else {
			driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		    //driver.findElement(By.tagName("H1"));
			//assertEquals("Авторизация", driver.getTitle());
			driver.findElement(By.id("username"))
				.sendKeys(user.getLogin());
			driver.findElement(By.id("password"))
				.sendKeys(user.getPassword());
			driver.findElement(By.name("login"))
				.click();
		}
	  }
	
	   
	@Override
	public void logout() {
	   //assertEquals("Интернет-магазин", driver.getTitle());
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.findElement(By.linkText("Выйти")).click();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	   //wait.until(alertIsPresent()).accept();
	}

	
	@Override
	public boolean isLoggedIn() {
	   return driver.findElements(By.linkText("Выйти")).size() > 0;
	}

	@Override
	public boolean isLoggedInAs(User user) {
		//return isLoggedIn() && this.user.getLogin().equals(user.getLogin());
		return isLoggedIn()
	        && getLoggedUser().getLogin().equals(user.getLogin());
			//&& driver.findElements(By.linkText("Вы зашли как"));
	}

	@Override
	public boolean isNotLoggedIn() {
		//return user == null;
		return driver.findElements(By.id("forgotpassword_email")).size() > 0;
	    //return driver.findElements(By.linkText("Войти")).size() > 0;
	}
	
	private User getLoggedUser() {
	   //manager.getNavigationHelper().gotoUserProfilePage();
	   return new User()
	     // .setLogin(driver.findElement(By.className("b-page-content_in")).getText(12,17));
	   .setLogin("slnsk");
	}

	@Override
	public void createUser(User user) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void changeUserInfo(User user) {
		// TODO Auto-generated method stub
		
	}

}
